package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.entity.Exchange;
import com.hy.entity.Product;
import com.hy.entity.User;
import com.hy.mapper.ProductMapper;
import com.hy.service.ExchangeService;
import com.hy.service.ProductService;
import com.hy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private UserService userService;

    @Autowired
    private ExchangeService exchangeService;

    @Override
    public boolean putOnSale(Long id) {
        Product product = getById(id);
        if (product == null) {
            return false;
        }
        product.setStatus(1);
        return updateById(product);
    }

    @Override
    public boolean putOffSale(Long id) {
        Product product = getById(id);
        if (product == null) {
            return false;
        }
        product.setStatus(0);
        return updateById(product);
    }

    @Override
    public boolean updateStock(Long id, Integer stock) {
        Product product = getById(id);
        if (product == null) {
            return false;
        }
        product.setStock(stock);
        return updateById(product);
    }

    @Override
    public boolean exchange(Long id, Long userId) {
        // 获取商品信息
        Product product = getById(id);
        if (product == null || product.getStatus() != 1 || product.getStock() <= 0) {
            return false;
        }

        // 获取用户信息
        User user = userService.getById(userId);
        if (user == null) {
            return false;
        }

        // 检查用户积分是否足够
        if (user.getPoints() < product.getPoints()) {
            return false;
        }

        // 创建兑换记录（包含扣减积分和减少库存的操作）
        return exchangeService.create(userId, id, user.getAddress());
    }

    @Override
    public Page<Product> pageProducts(Integer pageNum, Integer pageSize, Integer status,String name) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        // 可选条件：根据状态过滤
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        if (name != null){
            queryWrapper.like("name", name);
        }

        // 按创建时间倒序排列
        queryWrapper.orderByDesc("create_time");

        return page(page, queryWrapper);
    }

    @Override
    public boolean saveProduct(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        return save(product);
    }

    @Override
    public boolean updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        return updateById(product);
    }

    @Override
    public boolean updateProductStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        product.setUpdateTime(LocalDateTime.now());
        return updateById(product);
    }

}
